<?php
namespace app\admin\controller;

class Club extends Comm{
	//社区列表
	public function club_list(){
		if(input('get.type')){
			$info = model('club')->where('type="'.input('get.type').'"')->select();
			$count = model('club')->count();
			$data = [];
			foreach($info as $v){
				$data[] = $v->toArray();
			}
			$this->assign('count',$count);
			$this->assign('data',$data);
			return $this->fetch();
		}else{
			$info = model('club')->select();
			$count = model('club')->count();
			$data = [];
			foreach($info as $v){
				$data[] = $v->toArray();
			}
			$this->assign('count',$count);
			$this->assign('data',$data);
			return $this->fetch();
		}
		
		
	}

	//添加社区活动
	public function club_add(){
		if(request()->isPost()){
			$re = $this->validate(input('post.'),'Club');
			if($re !== true){
				$this->error($re);
			}
			$files = request()->file('thumb');
			$path = ROOT_PATH.'public'.DS.'static'.DS.'upload'.DS.'club';
			$info = $files->move($path);
				if($info){
					$savename = $info->getSavename();
					$data['thumb'] = $savename;
				}else{
					echo $file->getError();
				}
			$date = input('post.y').'年 '.input('post.m').'月 '.input('post.d').'日';
			$data['date'] = $date;
			$data['content'] = input('post.editorValue');
			$data = array_merge($data,input('post.'));
			$res = model('Club')->allowfield(true)->save($data);
			if($res>1){
				$this->success('添加成功');
			}
		}
		return $this->fetch();
	}
	public function club_edit(){

	}
	//添加参与
	public function join(){
		if(request()->isPost()){
			//更新数据
			$uname = model('club')->where('cid='.input('get.cid'))->find()->uname;
			if($uname != null || $uname != ''){
				$uname = json_decode($uname,true);
				if(is_string($uname)){
					$uname = (array)$uname;
				}
				$merge = (array)(input('post.uname').'-'.input('post.tel'));
				$uname = array_merge($uname,$merge);	//合并数据
				$data['uname'] = json_encode($uname);
				$info = model('club')->where('cid='.input('get.cid'))->update($data);
				if($info>0){
					$this->success('添加成功');
				}
			}else{
				$data['uname'] = json_encode(input('post.uname').'-'.input('post.tel'));
				$info = model('club')->where('cid='.input('get.cid'))->update($data);
				if($info>0){
					$this->success('添加成功');
				}
			}
			
		}
		$uname = model('club')->where('cid='.input('get.cid'))->find()->uname;
		$uname = json_decode($uname,true);
		$this->assign('join',$uname);
		return $this->fetch();
	}

	//上架
	public function club_start(){
		if(!session('?cid') || input('post.cid') != session('cid')){
			session('cid',input('post.cid'));
		}
		
		$data['status'] = 1;
		$info = model('club')->where('cid='.session('cid'))->update($data);
		if($info >= 1){
			$this->success('已上架');
		}else{
			$this->success('上架失败');
		}
	}
	//下架
	public function club_stop(){
		if(!session('?cid') || input('post.cid') != session('cid')){
			session('cid',input('post.cid'));
		}
		
		//dump(session('rid'));
		$data['status'] = 0;
		$info = model('club')->where('cid='.session('cid'))->update($data);
		if($info >= 1){
			$this->success('已下架');
		}else{
			$this->success('下架失败');
		}
	}

	//回复
	public function club_huifu(){
		// dump($_GET);exit;
		$data = model('Pin')->where(['cid'=>$_GET['id']])->find()->toArray();
		$this->assign('v',$data);
		// dump($data);exit;
		return  $this->fetch();
	}

	//处理回复
	public function do_pin(){
		// dump($_POST);exit;
		$res = model('Pin')->where(['id'=>$_POST['id']])->update(['status'=>$_POST['caozuo']]);

		if($res > 0){
			$this->success('修改成功');
		}
	}

	//回复评论
	public function do_pin2(){
		$res = model('Pin')->where(['id'=>$_POST['id']])->update(['huifu'=>$_POST['caozuo']]);

		if($res > 0){
			$this->success('回复成功');
		}
	}

}